<?php
/**
 * page.libaray.php
 * @Copyright lemonCarlJ All rights reserved.
 * @License http://www.56code.com/License.txt
 * @Version 1.0.0
 * @Email rpgshifa@aliyun.com
 */
namespace SF\libaray;

class page
{
    /**
     * 总条数
     * @var int
     */
    public $total;
    
    /**
     * 每页显示条数
     * @var int
     */
    public $size;
    
    /**
     * 当前页
     * @var int
     */
    protected $current = 1;
    
    /**
     * 总页数
     * @var int
     */
    protected $pages;
    
    //存储数据
    protected $data = array();
    
    /**
     * 模板规则
     * @var string
     * [total] 总条数
     * [current] 当前页
     * [size] 每页条数
     * [pages] 总页数
     * [comic] 首页
     * [prev] 上一页
     * [first] 开始几页
     * [first_more] 开始页更多
     * [before] 前几页
     * [locate] 当前位置
     * [after] 后几页
     * [last_more] 最后页更多
     * [last] 最后几页
     * [next] 下一页
     * [end] 尾页
     *
     * @special
     * {url} 分页url
     * {字段} 可被混淆码干扰字段
     */
    public $template = '共[total]条,每页[size]条, 当前第[current]/[pages]页, [comic][prev][first][first_more][before][locate][after][last_more][last][next][end]';
    
    /**
     * 分页Html
     * @var array
     */
    public $html = array(
        'total'   => '[total]',
        'current' => '[current]',
        'size'    => '[size]',
        'pages'   => '[pages]',
        'comic'   => '<a href="{url}{comic}">首页</a>',
        'prev'    => '<a href="{url}{prev}">上一页</a>',
        'first'   => '<a href="{url}{first}">[first]</a>',
        'first_more'   => '...',
        'before'  => '<a href="{url}{before}">[before]</a>',
        'locate'  => '<b>[locate]</b>',
        'after'   => '<a href="{url}{after}">[after]</a>',
        'last_more'    => '...',
        'last'    => '<a href="{url}{last}">[last]</a>',
        'next'    => '<a href="{url}{next}">下一页</a>',
        'end'     => '<a href="{url}{end}">尾页</a>'
    );
    
    /**
     * 基础URL地址
     * @var string
     */
    protected $url = '';
    
    /**
     * 混淆码
     * @var array
     */
    public $code = array();
    
    /**
     * 初始化分页
     * @param int $total 总条数
     * @param int $size 每页条数
     * @param string $url 分页URL
     */
    public function __construct( $total, $size, $url )
    {
        $this->total = (int)$total;
        $this->size = (int)$size;
        $this->url = $url;
        $this->pages = ceil( $this->total / $this->size );
        $this->pages = (int)$this->pages;
    }
    
    /**
     * 计算分页数据
     * @param number $current 当前页
     * @param number $before 前几页 [ 3 ]
     * @param number $after 后几页 [ 3 ]
     * @param number $first 开始几页 [ 0 ]
     * @param number $last 最后几页 [ 0 ]
     * @return \SF\libaray\page
     */
    public function compute( $current, $before = 3, $after = 3, $first = 0, $last = 0 )
    {
        //规范数据
        if( $current < 1 )
        {
            $current = 1;
        }
        
        $before = (int)$before;
        $after  = (int)$after;
        $first  = (int)$first;
        $last   = (int)$last;
        
        //初始化存储数据
        $data = array();
        
        $data[ 'total' ] = $this->total; //总页数
        $data[ 'current' ] = $current; //当前页
        $data[ 'size' ] = $this->size; //每页尺寸
        $data[ 'locate' ] = $current; //当位置
        $data[ 'pages' ] = $this->pages; //总页数
        $this->current = $current; //当前页
        
        //首页和上一页
        if( $current !== 1 )
        {
            $data[ 'comic' ] = '';
            $data[ 'prev' ] = $current - 1;
        }
        
        //尾页和下一页
        if( $current !== $this->pages )
        {
            $data[ 'next' ] = $current + 1;
            $data[ 'end' ]  = $this->pages;
        }
        
        //计算前页数
        if( $before > 0 )
        {
            if( ( $current - $before ) > 0 )
            {
                $data[ 'before' ] = range( $current - $before, $current - 1 );
            }else{
                if( $current !== 1 )
                {
                    $before = $current - 1;
                    $data[ 'before' ] = range( 1, $current - 1 );
                }else{
                    $before = 0;
                }
            }
        }
        
        
        //计算后页数
        if( $after > 0 )
        {
            if( ( $after + $current ) < $this->pages )
            {
                $data[ 'after' ] = range( $current + 1, $current + $after );
            }else{
                if( $current !== $this->pages )
                {
                    $after = $this->pages - $current;
                    $data[ 'after' ] = range( $current + 1, $this->pages );
                }else{
                    $after = 0;
                }
            }
        }

        //计算开始几页
        if( $first > 0 ){
            if( ( $first + $before + 1 ) < $current )
            {
                $data[ 'first' ] = range( 1, $first );
            }else{
                $first = $current - $before - 2;
                if( ( $before + 1 ) < $current )
                {
                    if( $first > 0 )
                    {
                        $data[ 'first' ] = range( 1, $first );
                    }
                    
                    if(  $first === 0)
                    {
                        $data[ 'first' ] = array( 1 );
                    }
                }
            }
        }
        
        if( isset($data[ 'first' ] ) )
        {
            $data[ 'first_more' ] = $this->html[ 'first_more' ];
        }
        
        //计算最后几页
        if( $last > 0 ){
            if( ( $last + $after + $current + 1 ) < $this->pages )
            {
                $data['last'] = range( $this->pages - $last + 1, $this->pages );
            }else{
                $last = $this->pages - $current - $after - 1;
                
                if( $last > 0 )
                {
                    $data[ 'last' ] = range( $this->pages - $last + 1, $this->pages );
                }
                
                if( $last === 0 )
                {
                    $data[ 'last' ] = array( $this->pages );
                }
            }
        }
        
        if( isset( $data[ 'last' ]) )
        {
            $data[ 'last_more' ] = $this->html[ 'last_more' ];
        }
        
        //存储数据
        $this->data = $data;
        
        return $this;
    }
    
    /**
     * 获取分页数据
     * @return array 返回分页数据
     */
    public function data()
    {
        return $this->data;
    }
    
    /**
     * 模板输出数据
     * @param string $always_show 总是显示分页 [ false ]
     */
    public function template( $always_show = false )
    {
        //是否显示
        if( $this->pages === 1 && !$always_show )
        {
            return ;
        }
        
        $data = $this->data; //获取数据
        $template = $this->template; //分页模板
        $html = $this->html; //字段模板
        $url = $this->url; //分页url
        $current = $this->current; //当前页码
        $pages = $this->pages; //总页数
        $code = $this->code; //混淆码
        
        //循环数据
        foreach ( $data as $tag_name => $tag_data )
        {
            if( !is_array( $tag_data ) )
            {
                $tag_data = array( $tag_data );
            }
            
            //临时存储模板
            $temp = '';
            $tag_tmp_name = '[' . $tag_name . ']';
            $tag_con_tmp_name = '{' . $tag_name . '}';
            foreach( $tag_data as $page )
            {
                //页码别名
                $confused_page = $page;
                if( is_array( $code ) )
                {
                    foreach( $code as $original => $confused )
                    {
                        $confused_page = str_replace( $original, $confused, $confused_page );
                    }
                }
                
                //获取标签值
                $tag_value = $html[ $tag_name ];
                
                //替换页码
                $tag_value = str_replace( $tag_tmp_name, $page, $tag_value );
                $tag_value = str_replace( $tag_con_tmp_name, $confused_page, $tag_value );
                
                $temp .= $tag_value;
            }
            
            //总模板替换
            $template = str_replace( $tag_tmp_name, $temp, $template );
        }
        
        //替换分页url
        $template = str_replace( '{url}', $url, $template );
        
        //处理不需要的标签
        foreach ( $html as $tag_name => $tag_html )
        {
            $tag_tmp_name = '[' . $tag_name . ']';
            $template = str_replace( $tag_tmp_name, '', $template );
        }
        
        return $template;
    }
}